package com.infore.airsupervise.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.bean.PolluterLicenseE;
import com.infore.airsupervise.biz.PolluterLicenseBiz;
import com.infore.airsupervise.biz.impl.PolluterLicenseBizImpl;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.ui.widget.SwipeRecyclerView;
import com.infore.airsupervise.utils.JumpUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @features: 排污许可证查询界面
 * @author: WuTao
 * @date: 2017/4/1 15:44
 */
public class LicenseSearchActivity
        extends BaseTitleBarActivity
        implements SwipeRecyclerView.OnActionListener {


    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.refreshRecyclerView)
    SwipeRecyclerView swipeRecyclerView;
    @BindView(R.id.tv_search)
    TextView tvSearch;

    private PolluterLicenseBiz polluterLicenseBiz;

    private BaseQuickAdapter<PolluterLicenseE, BaseViewHolder> quickAdapter;
    private List<PolluterLicenseE> datas = new ArrayList<>();

    private int searchType = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_polluter_search);
        ButterKnife.bind(this);

        polluterLicenseBiz = new PolluterLicenseBizImpl();

        initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        title.setText("排污许可证查询");
        rightMenu.setFontAwesomeIcon(FontAwesome.FA_FILTER);
        tvSearch.setText("搜索");
        // 键盘搜索按钮监听
        etSearch.setHint("搜索企业名称");
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    swipeRecyclerView.getSwipeLayout().setRefreshing(true);
                    swipeRecyclerView.onRefresh();
                    return true;
                }
                return false;
            }
        });

        // 适配器
        quickAdapter = new BaseQuickAdapter<PolluterLicenseE, BaseViewHolder>(R.layout.item_license_list, datas) {
            @Override
            protected void convert(BaseViewHolder helper, final PolluterLicenseE item) {
                helper.setText(R.id.company_name, item.getCompanyName());
                helper.setText(R.id.license_code, "许可证编号：" + item.getNumber());
                helper.setText(R.id.license_content, "许可内容：" + item.getContent());
            }
        };

        // 设置recyclerView
        swipeRecyclerView.setAdapter(quickAdapter);
        swipeRecyclerView.setActionListener(this);

        // 首次自动刷新
        swipeRecyclerView.getSwipeLayout().setRefreshing(true);
        swipeRecyclerView.onRefresh();
    }

    @Override
    public void onRefresh() {
        searchLicenseDatas(true);
    }

    @Override
    public void onLoadMore() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRecyclerView.getAdapter().loadMoreEnd();
                swipeRecyclerView.getSwipeLayout().setEnabled(true);
            }
        }, 1000);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        PolluterLicenseE item = datas.get(position);
        JumpUtil.jumpFromTo(ctx, LicenseDetailActivity.class, Constants.INTENT_OBJ_KEY, item);
    }

    /**
     * 搜索请求
     */
    private void searchLicenseDatas(final boolean isRefresh) {
        polluterLicenseBiz.searchPolluterLicense(searchType, etSearch.getText().toString(),
                new OnServerResponseListener<List<PolluterLicenseE>>() {
                    @Override
                    public void onSucceed(List<PolluterLicenseE> result, String desc) {
                        swipeRecyclerView.commonSuccessHandle(isRefresh, result.size());

                        datas.clear();
                        datas.addAll(result);
                        swipeRecyclerView.getAdapter().notifyDataSetChanged();
                    }

                    @Override
                    public void onFailed(int errorCode, String title, String desc) {
                        swipeRecyclerView.commonFailedHandle(isRefresh, errorCode, title, desc);
                    }
                });
    }

    @Override
    protected void onRightMenuClick(View view) {
        super.onRightMenuClick(view);

        PopupMenu popupMenu = new PopupMenu(ctx, rightMenu);
        popupMenu.getMenuInflater().inflate(R.menu.license_query_type, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.menu_company_name:
                        searchType = 1;
                        break;
                    case R.id.menu_mn:
                        searchType = 2;
                        break;
                    case R.id.menu_license_code:
                        searchType = 3;
                        break;
                }
                etSearch.setHint("搜索" + menuItem.getTitle());
                return true;
            }
        });
        popupMenu.show();
    }

    @OnClick(R.id.iv_searchClean)
    void onClearBtnClick() {
        etSearch.setText("");
    }

    @OnClick(R.id.tv_search)
    void onSearchBtnClick() {
        swipeRecyclerView.getSwipeLayout().setRefreshing(true);
        swipeRecyclerView.onRefresh();
    }
}
